package com.neusoft.carsale.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.neusoft.carsale.bean.PssException;
import com.neusoft.carsale.bean.User;
import com.neusoft.carsale.mapper.UserMapper;
import com.neusoft.carsale.service.UserService;
import com.neusoft.carsale.util.PasswordUtils;

/**
 * 
 * @author hp
 *
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl extends SuperServiceImpl<User> implements UserService {
	@Autowired
	private UserMapper userMapper;

	@Override
	public User login(User user) throws PssException {
		User user2 = userMapper.login(user);
		boolean ret = PasswordUtils.matches(user2.getSalt(), user.getPassword(), user2.getPassword());
		if (ret) {
			return user2;
		} else {
			return null;
		}

	}

	@Override
	public boolean add(User param) throws PssException {
		String salt=PasswordUtils.getSalt();
		String encodePassword=PasswordUtils.encode(param.getPassword(), salt);
		param.setSalt(salt);
		param.setPassword(encodePassword);
		boolean ret=userMapper.add(param);
		return ret;
	}

	@Override
	public boolean edit(User param) throws PssException {
		String salt=PasswordUtils.getSalt();
		String encodePassword=PasswordUtils.encode(param.getPassword(), salt);
		param.setSalt(salt);
		param.setPassword(encodePassword);
		boolean ret=userMapper.edit(param);
		return ret;
	}
	

}
